/*
 *	search-view view
 *  -----------------------------------------------------------------------------------------------------------
 */

site.view('search-view', {
	
	onLoad: function(settings) {
		this.startAll(settings);
	},
	
	onUnload: function() {
		this.stopAll();
	}

})

/*
 *	project-people-switcher
 *  -----------------------------------------------------------------------------------------------------------
 */

.module('project-people-switcher', function(context) {
	var query = context.query,
		TabControl = context.require('Tabs').TabControl,
		tabControl = new TabControl(),
		
		form_search_type;

	return {
		init: function(settings) {

			form_search_type = query('#search_type');

			tabControl.addTab('search-people', query('#btn_search_people').click(function() { 
				tabControl.changeTab('search-people');
				form_search_type.val(1); 
				return false;
			}), query('.people_division'));

			tabControl.addTab('search-project', query('#btn_search_projects').click(function() { 
				tabControl.changeTab('search-project'); 
				form_search_type.val(2);
				return false;
			}), query('.project_division'));
		}
	}
})

/*
 *	employed-switcher
 *  -----------------------------------------------------------------------------------------------------------
 */

.module('employed-switcher', function(context) {
	var query = context.query,
		
		valEmployed,
		valNotEmployed,
		btnEmployed,
		btnNotEmployed;

	return {
		init: function(settings) {

			valEmployed = query('#hidden_search_employed');
			valNotEmployed = query('#hidden_search_not_employed');
			btnEmployed = query('#btn_search_employed');
			btnNotEmployed = query('#btn_search_not_employed');

			btnEmployed.on('click', function() {
				valEmployed.val(+(!(+valEmployed.val())));
				btnEmployed.toggleClass('active');
					
				valNotEmployed.val(0);
				btnNotEmployed.removeClass('active');
			});

			btnNotEmployed.on('click', function() {
				valNotEmployed.val(+(!(+valNotEmployed.val())));
				btnNotEmployed.toggleClass('active', 1);

				valEmployed.val(0);
				btnEmployed.removeClass('active');
			});
		}
	}
})

/*
 *	scroll-manager
 *  -----------------------------------------------------------------------------------------------------------
 */

.module('scroll-manager', function(context) {
	var query = context.query,
		ajaxLoader,
		results;

	return {

		init: function() {
			ajaxLoader = query('#ajax_loader'),
			results = query('#content').find('section');

			query(window).scroll(function(){

				if (query(window).scrollTop() >= query(document).height() - query(window).height() * 1.3){
					context.trigger('scrollBottom');	
				}
			});
		},

		events: {

			searchStarted: function() {
				ajaxLoader.fadeIn('slow');
			},

			searchEnded: function() {
				ajaxLoader.hide();
			},

			contentLoaded: function(content) {
				results.append(content);
			}
		}
	}
})

/*
 *	scroll-loader
 *  -----------------------------------------------------------------------------------------------------------
 */

.module('scroll-loader', function(context) {
	var query = context.query,
		request = context.request,
		form_search_type =  query('#search_type'),

		form,
		formData,
		page = 2,
		processing = false,
		allDataLoaded = false,
		formSearchType = +form_search_type.val();

	return {
		init: function(settings) {
			
			form = query('#content').find('form');
			formData = form.serialize();

		},

		events: {
			scrollBottom: function() {

				if(allDataLoaded || processing) return;

				processing = true;
				context.trigger('searchStarted');

				request({
					url: '/search/index',
					type: 'post',
					dataType: 'json',
					data: formData + '&page=' + page,
					success: function(data) {
						
						if(data.success === true) {

							switch(formSearchType) {
								case 1: // people
									
									if(query(data.content).find('.list > li img').length === 0) {
										allDataLoaded = true;
										return;
									}

								break;

								case 2: // projects

									if(query(data.content).find('.box_result').length === 0) {
										allDataLoaded = true;
										return;
									}

								break;
							}
							

							context.trigger('contentLoaded', data.content);
							page++;
						}
					},

					complete: function() {
						processing = false;
						context.trigger('searchEnded');
					}
				});
				
			}
		}
	}
})
